package com.spring.mvc.rest;

import com.spring.mvc.config.vo.ResultVo;
import com.spring.mvc.entity.Person;
import com.spring.mvc.service.PersonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

/**
 * ${DESCRIPTION}
 *
 * @author roger
 * @email 190642964@qq.com
 * @create 2018-02-01 12:36
 **/
@RestController
@RequestMapping("/person")
public class PersonRest {

    private static final Logger LOGGER = LoggerFactory.getLogger(PersonService.class);

    @Autowired
    private PersonService personService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Person> get(@RequestParam(name = "id") Integer id) throws SQLException {
        LOGGER.debug("Controller 开始调用 Service");
        Person person = personService.findPerson(id);
        LOGGER.debug("Controller 调用完毕 Service");
        if (null == person) {
            return ResultVo.successResultIsNull();
        }
        return ResultVo.success(person);
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResultVo save(@RequestBody Person person) throws SQLException {
        personService.savePerson(person.getName());
        return ResultVo.success();
    }

    @RequestMapping(value = "/test/transaction", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo testTransactional() throws SQLException {
        personService.saveTestTransactional();
        return ResultVo.success();
    }
}
